package com.lc.zpyh.ui.activity.home;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.zpyh.R;
import com.lc.zpyh.app.AppActivity;
import com.lc.zpyh.http.request.MessagedetailsApi;
import com.lc.zpyh.http.response.MessagedetailsBean;
import com.lcodecore.tkrefreshlayout.utils.LogUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MessageDeatailsActivity extends AppActivity {
    @BindView(R.id.image_back)
    LinearLayout imageBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;


    @BindView(R.id.webView)
    WebView webView;
    private String id;

    private String newsTextSrart = "<html>" +
            "<head>" +
            "<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0'>" +
            "</head>" +
            "<body>";

    private String newsTextEnd = "<style type=\"text/css\"> img {" +
            "width: 100% !important;" +
            "height:auto !important;" +
            "}" +
            "</style>" +
            "</body></html>";
    @Override
    protected int getLayoutId() {
        return R.layout.activity_messagedeatails;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);


        id = getIntent().getStringExtra("id");

        messagedetails(id);

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);//设置webview推荐使用的窗口
        webSettings.setLoadWithOverviewMode(true);//设置webview加载的页面的模式
        webSettings.setDisplayZoomControls(false);//隐藏webview缩放按钮
        webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本
        webView.setWebViewClient( new  WebViewClient()
        {
            @Override
            public  void  onPageFinished(WebView view,String url)
            {

                LogUtil.i("webview加载结束");
            }
        });
    }

    private void messagedetails( String id) {
        EasyHttp.post(this)
                .api(new MessagedetailsApi().setId(id)).request(new HttpCallback<MessagedetailsBean>(this) {

            @Override
            public void onSucceed(MessagedetailsBean data) {
                if (data.getCode() == 0) {
                    tvTitle.setText(data.getNewsone().getTitle());

                    String content = data.getNewsone().getContent();

                    webView.loadDataWithBaseURL(null, newsTextSrart + content + newsTextEnd
                            , "text/html", "UTF-8", null);

                }

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    @OnClick(R.id.image_back)
    public void onClick() {
        finish();
    }
}
